using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace Stratman.DesktopProcess.DatabaseSynchronizer
{
    /// <summary>
    /// Writes trace information to a text stream in a slightly more abbreviated format than the
    /// standard TextWriterTraceListener class.
    /// </summary>
    public class BasicTextWriterTraceListener : TextWriterTraceListener
    {
        /// <summary>
        /// Default constructor for the class that calls the base constructor.
        /// </summary>
        public BasicTextWriterTraceListener()
            : base()
        {
        }

        /// <summary>
        /// Constructor that allows a filename to be passed in for initialization.
        /// </summary>
        /// <param name="fileName">
        /// Path to the file to which we are to write trace information.
        /// </param>
        public BasicTextWriterTraceListener(string fileName)
            : base(fileName)
        {
        }

        /// <summary>
        /// Constructor that allows a stream to be passed in for initialization.
        /// </summary>
        /// <param name="stream">
        /// Stream to which we are to write trace information.
        /// </param>
        public BasicTextWriterTraceListener(Stream stream)
            : base(stream)
        {
        }

        /// <summary>
        /// Writes a formatted trace event message string to the stream.
        /// </summary>
        /// <param name="eventCache">
        /// A TraceEventCache object that contains the current process ID, thread ID, and stack 
        /// trace information.
        /// </param>
        /// <param name="source">
        /// A name used to identify the output, typically the name of the application that 
        /// generated the trace event.
        /// </param>
        /// <param name="eventType">
        /// One of the TraceEventType values specifying the type of event that has caused the 
        /// trace.
        /// </param>
        /// <param name="id">
        /// A numeric identifier for the event.
        /// </param>
        /// <param name="format">
        /// A format string that contains zero or more format items, which correspond to objects in
        /// the args array.
        /// </param>
        /// <param name="args">
        /// An object array containing zero or more objects to format.
        /// </param>
        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
        {
            if (Filter.ShouldTrace(eventCache, source, eventType, id, format, args, null, null))
                WriteLine(String.Format("{0} ({1}) {2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), eventType.ToString("G"), String.Format(format, args)));
        }

        /// <summary>
        /// Writes a trace event message string to the stream.
        /// </summary>
        /// <param name="eventCache">
        /// A TraceEventCache object that contains the current process ID, thread ID, and stack 
        /// trace information.
        /// </param>
        /// <param name="source">
        /// A name used to identify the output, typically the name of the application that 
        /// generated the trace event.
        /// </param>
        /// <param name="eventType">
        /// One of the TraceEventType values specifying the type of event that has caused the 
        /// trace.
        /// </param>
        /// <param name="id">
        /// A numeric identifier for the event.
        /// </param>
        /// <param name="message">
        /// A message to write.
        /// </param>
        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
        {
            if (Filter.ShouldTrace(eventCache, source, eventType, id, message, null, null, null))
                WriteLine(String.Format("{0} ({1}) {2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), eventType.ToString("G"), message));
        }
    }
}
